<template>
	<view class="content">
		<u-swiper :list="slides" name="img_url" height="320"></u-swiper>

		<view class="u-text-center u-m-t-30">
			<u-tabs :list="list" bar-width="100" item-width="160" :current="current" @change="changeSort"></u-tabs>
		</view>

		<u-row gutter="16" class="u-skeleton">
			<u-col span="6" v-for="item in goodsList.length ? goodsList : 4">
				<goods-card :goods="item"></goods-card>
			</u-col>
		</u-row>
		<u-skeleton :loading="loading" :animation="true" bgColor="#FFF"></u-skeleton>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [{
					name: '默认'
				}, {
					name: '销量'
				}, {
					name: '推荐',
				}, {
					name: '最新',
				}],
				current: 0,
				slides: [],
				goodsList: [],
				pagination: {
					page: 1,
				},
				loading: false,
			}
		},
		async onLoad(option) {
			this.getData();
		},
		methods: {
			changeSort(index) {
				this.current = index;
				this.goodsList = [];
				this.pagination.page = 1;
				this.getData()
			},
			async getData() {
				this.loading = true;
				const params = {
					page: this.pagination.page
				}
				if (this.current === 1) params.sales = 1;
				if (this.current === 2) params.recommend = 1;
				if (this.current === 3) params.new = 1;

				let {
					slides,
					goods
				} = await this.$u.api.index(params);
				this.loading = false
				this.slides = slides;
				this.goodsList = [...this.goodsList, ...goods.data];
			},
		},
		onReachBottom() {
			this.pagination.page += 1;
			this.getData()
		}
	}
</script>

<style lang="scss" scoped>
	.goods-item {
		box-shadow: 0 10rpx 20rpx 0 rgba(0, 0, 0, .1);
	}
</style>
